package com.mdxq.mall.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mdxq.mall.product.entity.ProductCategoryAttributeRelation;
import com.mdxq.mall.product.mapper.ProductCategoryAttributeRelationMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mdxq.mall.product.entity.ProductCategory;
import com.mdxq.mall.product.mapper.ProductCategoryMapper;
import com.mdxq.mall.product.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品分类 服务层实现。
 *
 * @author zdy
 * @since 2025-01-12
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>  implements ProductCategoryService{

    @Override
    public List<ProductCategory> listWithChildren() {
        QueryWrapper queryWrapper = new QueryWrapper();
        //一级分类
        queryWrapper.select("id","name");
        queryWrapper.eq("parent_id",0);//        queryWrapper.eq("level",0);也可
        queryWrapper.eq("show_status",1);//消除掉无需显示的分类
        List<ProductCategory> categoryList = mapper.selectListByQuery(queryWrapper);
        for (ProductCategory productCategory : categoryList) {
            //二级分类
            queryWrapper.clear();
            queryWrapper.select("id","name","parent_id");
            queryWrapper.eq("parent_id",productCategory.getId());
            queryWrapper.eq("show_status",1);
            productCategory.setChildren(mapper.selectListByQuery(queryWrapper));
        }
        return categoryList;
    }

    @Autowired
    private ProductCategoryAttributeRelationMapper attributeRelationMapper;
    //重写save方法
    @Override
    @Transactional
    public boolean save(ProductCategory entity) {
        //parentid是0就是一级，其他就是2级
        entity.setLevel(entity.getParentId() == 0 ? 0 : 1);
        entity.setProductCount(0);//初始化商品数量
        boolean ret = super.save(entity);
        if (ret&& CollUtil.isNotEmpty(entity.getProductAttributeIdList())){
            //添加关联表的数据
            List<ProductCategoryAttributeRelation> relationList = new ArrayList<>();
            entity.getProductAttributeIdList().forEach(attrId->{
                //封装relation实例，添加列表里
                relationList.add(new ProductCategoryAttributeRelation(null, entity.getId() ,attrId));
            });
            attributeRelationMapper.insertBatch(relationList);//批量添加关联记录
        }
        return ret;
    }

    @Override
    @Transactional
    public boolean updateById(ProductCategory entity) {
        entity.setLevel(entity.getParentId() == 0 ? 0 : 1);
        boolean ret = super.updateById(entity);
        if (ret){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("product_category_id",entity.getId());
            //先删除有关的记录
            attributeRelationMapper.deleteByQuery(queryWrapper);
            //如果关联的商品属性id的列表不为空，就把关联的记录批量添加进去
            if (CollUtil.isNotEmpty(entity.getProductAttributeIdList())){
                //添加关联表的数据
                List<ProductCategoryAttributeRelation> relationList = new ArrayList<>();
                entity.getProductAttributeIdList().forEach(attrId->{
                    //封装relation实例，添加列表里
                    relationList.add(new ProductCategoryAttributeRelation(null, entity.getId() ,attrId));
                });
                attributeRelationMapper.insertBatch(relationList);//批量添加关联记录
            }
        }
        return ret;
    }
}
